//
//  ViewController.m
//  JKOCFilePathOperation
//
//  Created by 王冲 on 2018/4/17.
//  Copyright © 2018年 希艾科技有限公司. All rights reserved.
//

#import "ViewController.h"
#import "JKFilePathOperationExtension.h"
@interface ViewController ()

@property(nonatomic,strong) UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    NSLog(@"%@",[JKFilePathOperationExtension jKHomeDirectory]);
    
    [self jKCalculateTheSizeOfTheFolderPath];
   
    
}

#pragma mark 计算文件或者文件夹的大小
-(void)jKCalculateTheSizeOfTheFolderPath{
    
    NSString *path = [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()];
    
    NSString *sizeStr = [[JKFilePathOperationExtension new]jKCalculateTheSizeOfTheFolderPath:path];
    
    NSLog(@"文件的大小=%@",sizeStr);
}

#pragma mark 查看文件的属性
-(void)jKGetFileAttributesFilePath{
    
    NSString *path = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"我的笔记.text"];
    
    // jKGetFileAttributesFilePath
    NSDictionary *fileAttributes = [JKFilePathOperationExtension jKGetFileAttributesFilePath:path];
    
    if (fileAttributes != nil) {
        NSNumber *fileSize;
        NSString *fileOwner, *creationDate;
        NSDate *fileModDate;
        //NSString *NSFileCreationDate
        //文件大小
        if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
            
            NSLog(@"文件的大小= %qi\n", [fileSize unsignedLongLongValue]);
        }
        //文件创建日期
        if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) {
            
            NSLog(@"文件创建的日期: %@\n", creationDate);
        }
        //文件所有者
        if ((fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName])) {
            
            NSLog(@"Owner: %@\n", fileOwner);
        }
        //文件修改日期
        if ((fileModDate = [fileAttributes objectForKey:NSFileModificationDate])) {
            
            NSLog(@"文件修改的日期: %@\n", fileModDate);
        }
    }
    else {
        NSLog(@"该文件不存在");
    }
    
}

#pragma mark 取出图片
-(void)jKReadImage{
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 100)];
    [self.view addSubview:self.imageView];
    
    
    
    NSString *imageStr = @"http://images.ciotimes.com/o_1can10mm91sd91c6n1thv15oel8g9.png";
    
    //[JKFilePathOperationExtension jKwriteImageToFilePath:[JKFilePathOperationExtension jKTmp] withImageUrl:imageStr];
    
    __weak typeof(self) weakSelf = self;
    [JKFilePathOperationExtension jKReadImageWithImageUrl:imageStr withReadImage:^(UIImage *image, BOOL readImageStatus) {
        
        if (readImageStatus) {
            
            weakSelf.imageView.image = image;
        }else{
            
            NSLog(@"没有图片");
        }
    }];
    
}

#pragma mark  移动文件
-(void)moveFile{
    
    NSString *fromPath = [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/Documents/JKPdf"];
    NSString *toPath = [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/tmp/JKPdf"];
    BOOL isSuccess = [[JKFilePathOperationExtension new]jKmoveFileFromPath:fromPath withToPath:toPath];
    
    if (isSuccess) {
        
        NSLog(@"移动成功");
    }else{
        NSLog(@"移动失败");
    }
}

#pragma mark  复制文件
-(void)jKCopyFromFile{
    
    NSString *fromPath = [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/Documents/我的笔记.text"];
    NSString *toPath = [NSString stringWithFormat:@"%@%@",NSHomeDirectory(),@"/Documents/复制后的笔记.text"];
   BOOL isSuccess = [[JKFilePathOperationExtension new]jKCopyFromFilePath:fromPath withCopyToFilePath:toPath];
    
    if (isSuccess) {
        
        NSLog(@"复制成功");
    }else{
        NSLog(@"复制失败");
    }
}

#pragma mark 写入文件
-(void)jKWriteToFile{
   
    /*  字符串 1
     */
    NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/我的笔记.text"];
    NSString *content = @"笔记在于记录美好的生活。";
    
    /*  图片 2
     
       // 图片的名字
       NSString *imageString = @"testimage.png";
       UIImage *image = [UIImage imageNamed:imageString];
       NSData *data = UIImagePNGRepresentation(image);
       // 图片的存储文件夹
       NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/JKImage"];
       NSString *imagePath = [[JKFilePathOperationExtension new]jKCreateFolder:customPath];
       // 图片的存储路径
       imagePath = [NSString stringWithFormat:@"%@/%@",imagePath,imageString];
     */

    /* 数组 3
      // 先有数组的文件
      NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/array.plist"];
      NSString *arrayPath = [[JKFilePathOperationExtension new]jKCreateFile:customPath];
      // 创建数组
      NSArray  *array = @[@"1",@"2",@"3"];
     */

    /* 字典 4
      // 先有字典的文件
      NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/dictionary.plist"];
      NSString *dictPath = [[JKFilePathOperationExtension new]jKCreateFile:customPath];
      // 创建字典
      NSDictionary *dict = @{@"1":@"9",@"2":@"8",@"3":@"7",@"4":@"6"};
     */
    
    [[JKFilePathOperationExtension new]jKWriteToFile:customPath withWriteToType:1 withWriteToContent:content];
    
}

#pragma mark 创建文件
-(void)jKCreateFile{
    
    NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/我的笔记.text"];
    NSString *path = [[JKFilePathOperationExtension new]jKCreateFile:customPath];
    NSLog(@"path========%@",path);
}

#pragma mark 4、对指定路径执行浅搜索，返回指定目录路径下的文件、子目录及符号链接的列表
-(void)readAllFiles{
    
    NSString *customPath = [NSString stringWithFormat:@"%@",[JKFilePathOperationExtension jKHomeDirectory]];
    NSLog(@"所有文件数组=%@",[[JKFilePathOperationExtension new]jKShallowSearchAllFiles:customPath]);
}

#pragma mark 3.深度搜索遍历一（subpathsAtPath）深度遍历，会递归遍历子文件夹（包括符号链接，所以要求性能的话用enumeratorAtPath）
-(void)jKGetAllFileNames{
    
    NSString *customPath = [NSString stringWithFormat:@"%@",[JKFilePathOperationExtension jKHomeDirectory]];
    NSLog(@"所有文件数组=%@",[[JKFilePathOperationExtension new]jKDeepSearchSubpathsAtPathAllFileNames:customPath]);
}

#pragma mark 2、删除文件夹
-(void)deleteFile{
    
    NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/JKFile"];
    [[JKFilePathOperationExtension new]jKRemovefolder:customPath];
}
#pragma mark 1、在Documents下创建JKFile文件夹
-(void)createFile{
    
    NSString *customPath = [NSString stringWithFormat:@"%@%@",[JKFilePathOperationExtension jKDocuments],@"/JKFile"];
    NSString *path = [[JKFilePathOperationExtension new]jKCreateFolder:customPath];
    NSLog(@"path========%@",path);
}


@end
